package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService {

    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private StatementMapper statementMapper;


    @Override
    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<StatementItem>(statementItemMapper.selectForList(qo));
    }


    @Override
    public void save(StatementItem statementItem) {
        statementItemMapper.insert(statementItem);
    }

    @Override
    public StatementItem get(Long id) {
        return statementItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(StatementItem statementItem) {
        statementItemMapper.updateByPrimaryKey(statementItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<StatementItem> list() {
        return statementItemMapper.selectAll();
    }

    @Override
    @Transactional
    public void saveItems(List<StatementItem> list) {
        StatementItem statementItem = list.remove(list.size() - 1);
        BigDecimal disCountAmount = statementItem.getItemPrice();
        Long statementId = statementItem.getStatementId();
        BigDecimal totalAmount = new BigDecimal("0");
        BigDecimal totalCount = new BigDecimal("0");
        statementItemMapper.deleteRelation(statementId);
        for (StatementItem item : list) {
            totalAmount = totalAmount.add(item.getItemPrice().multiply(item.getItemQuantity()));
            totalCount = totalCount.add(item.getItemQuantity());
            statementItemMapper.insert(item);
        }
        statementMapper.updateAmount(totalAmount,totalCount,disCountAmount,statementId);
    }

    @Override
    @Transactional
    public void pay(Long statementId) {
        Statement statement = statementMapper.selectByPrimaryKey(statementId);
        if (!statement.getStatus().equals(Statement.STATUS_CONSUME)) {
            throw new BusinessException("非法操作");
        }
        statementMapper.updatePayStatus(statementId,Statement.STATUS_PAID,ShiroUtils.getUserId());
    }

    @Override
    public Workbook exportFile(StatementItemQuery qo) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("计算清单明细");
        List<StatementItem> statementItems = statementItemMapper.selectForList(qo);
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("服务项名称");
        row.createCell(1).setCellValue("服务项价格");
        row.createCell(2).setCellValue("购买数量");
            for (int i = 0 ;i < statementItems.size();i++){
                Row row1 = sheet.createRow(i + 1);
                row1.createCell(0).setCellValue(statementItems.get(i).getItemName());
                row1.createCell(1).setCellValue(statementItems.get(i).getItemPrice().doubleValue());
                row1.createCell(2).setCellValue(statementItems.get(i).getItemQuantity().intValue());
            }

        return workbook;
    }

}
